package com.hlz.lib.effectivejava.chapter2;

import java.util.EnumSet;
import java.util.Set;

/**
 * 建造者模式的类层次结构
 *
 * @author Hongliang Zhu
 * @create 2023-08-20 14:17
 */
public abstract class Pizza {
    public enum Topping {HAM, MUSHROOM, ONION, PEPPER, SAUSAGE}

    final Set<Topping> toppings;

    abstract static class Builder<T extends Builder<T>> {
        EnumSet<Topping> toppings = EnumSet.noneOf(Topping.class);

        public T addToppings(Topping topping) {
            toppings.add(topping);
            return self();
        }

        abstract Pizza build();

        protected abstract T self();

    }

    public Pizza(Builder<?> builder) {
        // 保护性clone
        this.toppings = builder.toppings.clone();
    }
}
